package com.xunshangwang.pinhaohuo.adapters;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Group;
import com.xunshangwang.pinhaohuo.entity.User;
import com.xunshangwang.pinhaohuo.transformation.CircleTransformation;

import java.text.SimpleDateFormat;
import java.util.Date;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/3/2.
 */
public class GroupUserAdapter extends BaseRecyclerAdapter<User>{
    int mLostUserNum;
    public GroupUserAdapter(Context context,Group group) {
        super(context,group.getUsers());
        mLostUserNum=0;
        if(group.getOrder()!=null && group.getOrder().getOrder_goods()!=null){
            mLostUserNum=group.getOrder().getOrder_goods().getGroup_number()-getListSize();
        }
    }

    @Override
    public int getItemCount() {
        if(getListSize()==0)
            return super.getItemCount();
        return super.getItemCount()+1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.group_user_item,parent,false);
        return new UserHolder(view);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        UserHolder userHolder= (UserHolder) holder;
        if(position==1){
            userHolder.floorText.setVisibility(View.INVISIBLE);
            userHolder.dataText.setText("还差"+mLostUserNum+"人,让小伙伴们都来组团吧!");
            userHolder.dataText.setTextColor(ContextCompat.getColor(getContext(),R.color.dark_blue));
            userHolder.userHeadImage.setImageResource(R.drawable.group_user_default_image);
        }else {
            userHolder.dataText.setTextColor(ContextCompat.getColor(getContext(),R.color.dark));
            userHolder.floorText.setVisibility(View.VISIBLE);

            if(position>0)
                position=getListSize()-position+1;
            User user=getList().get(position);
            userHolder.floorText.setText(position == 0 ? "团长" : position + "楼");
            SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String joinTime=format.format(new Date(user.getJoin_time() * 1000));
            userHolder.dataText.setText(joinTime+"\n"+user.getNickname()+(position==0?" 发起拼团":" 成功加入"));
            Glide.with(getContext()).load(user.getAvatar()).placeholder(R.drawable.group_user_default_image)
                    .error(R.drawable.group_user_default_image)
                    .transform(new CircleTransformation(getContext(),R.dimen.group_user_list_head_border,
                    (position==0?R.color.red:R.color.white))).into(userHolder.userHeadImage);
        }
    }
    private class UserHolder extends RecyclerView.ViewHolder{
        ImageView userHeadImage;
        TextView floorText,dataText;
        public UserHolder(View itemView) {
            super(itemView);
            userHeadImage= (ImageView) itemView.findViewById(R.id.group_user_head_image);
            floorText= (TextView) itemView.findViewById(R.id.group_user_floor_text);
            dataText= (TextView) itemView.findViewById(R.id.user_data_text);
        }
    }
}
